package jy.game;

import android.telephony.mbms.FileInfo;

import java.util.ArrayList;
import java.util.List;

import jy.game.tools.CommonSettings;
import wx.xml.simpleframework.xml.Attribute;
import wx.xml.simpleframework.xml.Element;
import wx.xml.simpleframework.xml.Root;


public class AssetBundleManager {

    public static String  CurrentLoadingAssetsInfo = "";
    //        public static void Init(MonoBehaviour parent, CommonSettings.VoidCallBack callback) {
//            AssetBundleManager._parent = parent;
//            AssetBundleManager._callback = callback;
//            AssetBundleManager.Clear();
//            if (CommonSettings.MOD_KEY >= 0) {
//                AssetBundleManager.LoadUserDefinedAnimations(delegate {
//                    AssetBundleManager.LoadModResources(delegate {
//                        JyResourceManager.detail = "正在载入本地资源..";
//                        AssetBundleManager.LoadAssetBundles();
//                    });
//                });
//            } else {
//                AssetBundleManager.LoadAssetBundles();
//            }
//        }
    public static boolean IsInited                 = false;
    public static boolean IsFailed                 = false;


//        public static void LoadModResources(CommonSettings.VoidCallBack callback) {
//            String url = ModManager.ModBaseUrl + "/index.xml";
//            Tools.getFileContentFromUrl(AssetBundleManager._parent, url, delegate (String str) {
//                AssetBundleManager.ModResourcesData modResourcesData = Tools.DeserializeXML<AssetBundleManager.ModResourcesData>(str);
//                if (modResourcesData != null) {
//                    AssetBundleManager.data = modResourcesData;
//                }
//                boolean  flag = AssetBundleManager.loadPreCache();
//                for (AssetBundleManager.ModResourceData modResourceData:AssetBundleManager.data.data1) {
//                    if (!modResourceData.url.StartsWith("Animations/", )) {
//                        AssetBundleManager._tobeLoadFiles.add(modResourceData.url);
//                        if (flag) {
//                            if (Application.isMobilePlatform) {
//                                if (ModEditorResourceManager.spriteCache.Count < 501) {
//                                    ModEditorResourceManager.preCacheSprite(modResourceData);
//                                }
//                            } else {
//                                ModEditorResourceManager.preCacheSprite(modResourceData);
//                            }
//                        }
//                    }
//                }
//                AssetBundleManager.LoadNextModResource(callback);
//            }, null);
//        }
//
//
//        public static void LoadUserDefinedAnimations(CommonSettings.VoidCallBack callback) {
//            if (CommonSettings.MOD_KEY >= 0 && CommonSettings.PRECACHE_TEXTURE() == 1) {
//                JyResourceManager.progress = 0.5f;
//                JyResourceManager.detail = "正在预加载图片资源...";
//            } else {
//                JyResourceManager.progress = 0f;
//                JyResourceManager.detail = "正在载入MOD定义动画...";
//            }
//            UserDefinedAnimationManager.instance.Init(callback);
//        }
//
//
//        public static void LoadNextModResource(CommonSettings.VoidCallBack callback) {
//            if (AssetBundleManager._currentLoadResource >= AssetBundleManager._tobeLoadFiles.Count) {
//                callback();
//                return;
//            }
//            String text = AssetBundleManager._tobeLoadFiles[AssetBundleManager._currentLoadResource];
//            if (AssetBundleManager._tobeLoadFiles.Count > 0) {
//                JyResourceManager.progress = _currentLoadResource / (double)_tobeLoadFiles.Count;
//                JyResourceManager.detail = "正在载入MOD资源 " + text;
//            }
//            if (text.EndsWith(".png", ) || text.EndsWith(".jpg", )) {
//                ModEditorResourceManager.AddSprite(text.split(new char[]
//                {
//                    '.'
//                })[0], text);
//                AssetBundleManager._currentLoadResource++;
//                AssetBundleManager.LoadNextModResource(callback);
//                return;
//            }
//            if (text.EndsWith(".ogg", ) || text.EndsWith(".mp3", )) {
//                if (!Application.isMobilePlatform) {
//                    text = text.Replace(".mp3", ".ogg");
//                } else {
//                    text = text.Replace(".ogg", ".mp3");
//                }
//                ModEditorResourceManager.AddAudio(text.split(new char[]
//                {
//                    '.'
//                })[0], text);
//                AssetBundleManager._currentLoadResource++;
//                AssetBundleManager.LoadNextModResource(callback);
//                return;
//            }
//            if (text.EndsWith(".xml", )) {
//                AssetBundleManager._parent.StartCoroutine(ModEditorResourceManager.LoadXml(AssetBundleManager._parent.GetComponent<LoadingUI>(), text, delegate {
//                    AssetBundleManager.LoadNextModResource(callback);
//                }));
//                AssetBundleManager._currentLoadResource++;
//                return;
//            }
//            AssetBundleManager._currentLoadResource++;
//            AssetBundleManager.LoadNextModResource(callback);
//        }
//
//
//        private static IEnumerator InitAssetBundle(String name, String info, CommonSettings.VoidCallBack callback, CommonSettings.VoidCallBack failCallback = null) {
//            String path = Path.Combine(AssetBundleManager.baseUrl, name);
//            // name == "xml";
//            // FileLogger.instance.Log("downloading " + path);
//            WWW www = WWW.LoadFromCacheOrDownload(path, 0);
//            AssetBundleManager.CurrentWWW = www;
//            AssetBundleManager.CurrentLoadingAssetsInfo = info;
//            yield return www;
//            if (!TextUtils.isEmpty(www.error)) {
//                Logger.e(www.error);
//                if (failCallback != null) {
//                    failCallback();
//                }
//            } else {
//                switch (name) {
//                    case "maps":
//                        AssetBundleManager.MapAssets = www.assetBundle;
//                        break;
//                    case "battlebg":
//                        AssetBundleManager.BattleAssets = www.assetBundle;
//                        break;
//                    case "audios":
//                        AssetBundleManager.AudioAssets = www.assetBundle;
//                        break;
//                    case "xml":
//                        AssetBundleManager.XmlAssets = www.assetBundle;
//                        break;
//                    case "cgs":
//                        AssetBundleManager.CGAssets = www.assetBundle;
//                        break;
//                }
//                if (callback != null) {
//                    callback();
//                }
//            }
//            yield break;
//        }
//
//
//        public static Sprite GetMap(String url) {
//            if (AssetBundleManager.MapAssets != null) {
//                return AssetBundleManager.MapAssets.LoadAsset<Sprite>(url);
//            }
//            return null;
//        }
//
//
//        public static Sprite GetBattleBg(String url) {
//            if (AssetBundleManager.BattleAssets != null) {
//                return AssetBundleManager.BattleAssets.LoadAsset<Sprite>(url);
//            }
//            return null;
//        }
//
//
//        public static AudioClip GetAudio(String url) {
//            if (AssetBundleManager.AudioAssets != null) {
//                return AssetBundleManager.AudioAssets.LoadAsset<AudioClip>(url);
//            }
//            return null;
//        }
//
//
//        public static Sprite GetCG(String url) {
//            if (AssetBundleManager.CGAssets != null) {
//                return AssetBundleManager.CGAssets.LoadAsset<Sprite>(url);
//            }
//            return null;
//        }
//
//
//        public static String GetXml(String url) {
//            if (AssetBundleManager.XmlAssets != null) {
//                return (AssetBundleManager.XmlAssets.LoadAsset(url) as TextAsset).text;
//            }
//            return null;
//        }
//
//
//        private static boolean  loadPreCache() {
//            if (CommonSettings.MOD_KEY >= 0 && !ModEditorResourceManager.isPreCacheSprites) {
//                ModEditorResourceManager.isPreCacheSprites = true;
//                if (CommonSettings.PRECACHE_TEXTURE() == 1) {
//                    return true;
//                }
//            }
//            return false;
//        }


    //        public static AssetBundle MapAssets = null;
//
//
//        public static AssetBundle BattleAssets = null;
//
//
//        public static AssetBundle AudioAssets = null;
//
//
//        public static AssetBundle XmlAssets = null;
//
//
//        public static AssetBundle CGAssets = null;
//
//
//        public static WWW CurrentWWW = null;
    public static  AssetBundleManager.ModResourcesData data;
    private static CommonSettings.VoidCallBack         _callback;
    private static List<FileInfo>                      _tobeLoadResources = new ArrayList<>();
    private static List<String>                        _tobeLoadFiles     = new ArrayList<>();


    //        private static MonoBehaviour _parent;
    private static String[] avaliableExtensions  = {
        ".jpg",
        ".png",
        ".xml",
        ".ogg",
        ".mp3"
    };
    private static int      _currentLoadResource = 0;

    // (get) Token: 0x0600005F RID: 95 RVA: 0x0000D210 File Offset: 0x0000B410
    public static String baseUrl() {
        {
            return "";
//                if (Application.platform == RuntimePlatform.Android) {
//                    return "jar:file://" + Application.dataPath + "!/assets/";
//                }
//                if (Application.platform == RuntimePlatform.IPhonePlayer) {
//                    return "file://" + Application.dataPath + "/Raw/";
//                }
//                if (Application.platform == RuntimePlatform.WindowsPlayer || Application.isEditor) {
//                    return "file://" + Application.dataPath + "/StreamingAssets/";
//                }
//                if (Application.isWebPlayer) {
//                    return "StreamingAssets/";
//                }
//                return "file://" + Application.dataPath + "/StreamingAssets/";
        }
    }

    private static void LoadAssetBundles() {
//            MonoBehaviour parent = AssetBundleManager._parent;
//            CommonSettings.VoidCallBack callback = AssetBundleManager._callback;
//            parent.StartCoroutine(AssetBundleManager.InitAssetBundle("maps", "地图", delegate {
//                parent.StartCoroutine(AssetBundleManager.InitAssetBundle("battlebg", "战斗场景", delegate {
//                    parent.StartCoroutine(AssetBundleManager.InitAssetBundle("audios", "音乐音效", delegate {
//                        parent.StartCoroutine(AssetBundleManager.InitAssetBundle("cgs", "CG", delegate {
//                            parent.StartCoroutine(AssetBundleManager.InitAssetBundle("xml", "脚本", delegate {
//                                AssetBundleManager.IsInited = true;
//                                callback();
//                            }, delegate {
//                                AssetBundleManager.IsFailed = true;
//                            }));
//                        }, null));
//                    }, null));
//                }, null));
//            }, null));
    }

    private static void Clear() {
//            AssetBundleManager._tobeLoadResources.Clear();
//            AssetBundleManager._tobeLoadFiles.Clear();
//            AssetBundleManager._currentLoadResource = 0;
//            ModEditorResourceManager.Clear();
//            if (AssetBundleManager.MapAssets != null) {
//                AssetBundleManager.MapAssets.Unload(false);
//            }
//            if (AssetBundleManager.BattleAssets != null) {
//                AssetBundleManager.BattleAssets.Unload(false);
//            }
//            if (AssetBundleManager.AudioAssets != null) {
//                AssetBundleManager.AudioAssets.Unload(false);
//            }
//            if (AssetBundleManager.XmlAssets != null) {
//                AssetBundleManager.XmlAssets.Unload(false);
//            }
//            if (AssetBundleManager.CGAssets != null) {
//                AssetBundleManager.CGAssets.Unload(false);
//            }
    }

    @Root(name = "root")
    public class ModResourcesData {
        @Element(name = "data")
        public ModResourceData[] data1;
    }

    // [XmlType]
    public class ModResourceData {
        @Attribute
        public String url;
        @Attribute
        public String hash;

        @Attribute
        public int     w;
        @Attribute
        public int     h;
        @Attribute
        public boolean cache;
    }
}
